package init

import (
	"fmt"
	"github.com/spf13/viper"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
	"rpc-server/basic/config"
	"rpc-server/handler/model"
)

func init() {
	InitConfig()
	InitMysql()
}

func InitConfig() {
	viper.SetConfigFile("./config.yaml")
	err := viper.ReadInConfig()
	if err != nil {
		panic(err)
	}
	viper.Unmarshal(&config.Conf)
	log.Println("init config success")

}
func InitMysql() {
	var err error
	im := config.Conf.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/zg4?charset=utf8mb4&parseTime=True&loc=Local",
		im.User, im.Pass, im.Host, im.Port)
	config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		panic(err)
	}
	config.DB.AutoMigrate(model.Video{})
	log.Println("init mysql success")
}
